package com.situ.web.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.entity.User;
import com.situ.web.service.UserService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
	private UserService userService = new UserService();
	   @Override
	   protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 req.setCharacterEncoding("utf-8");
	     String name = req.getParameter("name");
	     String password = req.getParameter("password");
	     
	     User user = userService.login(name, password);
	     // 如果存在这样一个User对象，这个用户登录成功
	     if (user != null) {
	        // 将这个用户信息放到Session，判断用户是否登录就是根据Session里面有没有
	        // 这个用户信息
	        HttpSession session = req.getSession();
	        session.setAttribute("user", user);
	        // 登录完成之后重定向到学生列表界面
	        resp.sendRedirect(req.getContextPath() + "/student?method=findByPage");
	     } else {// 登录失败
	        resp.sendRedirect(req.getContextPath() + "/fail.jsp");
	     }
	   }
	}